import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import { layerProjection, tileGrid } from "./view";

/** 天地图API密钥 */
export const tk = "07086b15f474e5c9feacb786b61b4ecc";

const getTiandituUrl = (name: string): string => {
    const request = name.split("_");
    return `http://t0.tianditu.gov.cn/${name}/wmts?tk=${tk}&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=${request[0]}&STYLE=default&TILEMATRIXSET=${request[1]}&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}`;
};

/** 天地图图层文档: http://lbs.tianditu.gov.cn/server/MapService.html */
export const layersOfVector = [
    new TileLayer({ // 天地图矢量图层
        source: new XYZ({
            url: getTiandituUrl("vec_w"),
            projection: layerProjection, // openlayers 此处默认是 'EPSG:3857'
            tileGrid, // 添加tileGrid配置
            wrapX: true, // 左右滑动时, 超出世界地图范围后是否继续平铺, 较为影响性能
        }),
        preload: Infinity, // 预加载所有瓦片
    }),
    new TileLayer({ // 天地图矢量注记图层
        source: new XYZ({
            url: getTiandituUrl("cva_w"),
            projection: layerProjection,
            tileGrid,
            wrapX: true,
        }),
        preload: Infinity,
    }),
];

/** 天地图图层文档: http://lbs.tianditu.gov.cn/server/MapService.html */
export const layersOfImage = [
    new TileLayer({ // 天地图影像图层
        source: new XYZ({
            url: getTiandituUrl("img_w"),
            projection: layerProjection,
            tileGrid, // 添加tileGrid配置
            wrapX: false,
        }),
        preload: Infinity,
    }),
    new TileLayer({ // 天地图影像注记图层
        source: new XYZ({
            url: getTiandituUrl("cia_w"),
            projection: layerProjection,
            tileGrid, // 添加tileGrid配置
            wrapX: false,
        }),
        preload: Infinity,
    }),
];

/** 定义固定颜色 */
export const fixedColors = [
    "#ff0000",
    "#00ff00",
    "#0000ff",
    "#ffff00",
    "#ff00ff",
    "#00ffff",
    "#ff8000",
    "#8000ff",
    "#00ff80",
    "#ff0080",
    "#80ff00",
    "#0080ff",
    "#ff80ff",
    "#80ffff",
    "#ffff80",
    "#800000",
    "#008000",
    "#000080",
    "#800080",
    "#808000",
    "#8000a0",
    "#a00080",
    "#a08000",
    "#00a080",
    "#00a0a0",
    "#a00000",
    "#00a000",
    "#0000a0",
    "#a08080",
    "#a0a000",
    "#80a000",
    "#a000a0",
    "#808080",
    "#c0c0c0",
    "#808000",
    "#808040",
    "#40ff00",
    "#408000",
    "#4040ff",
    "#400080",
];
